<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width: 500px;
            height: 500px;
            background-color: red;
            margin: 50px;
            /* float: left; */
        }
        div:nth-child(4){
            background-color: pink;
        }


    </style>
</head>

<body>
    <button>开关1</button>
    <button>开关2</button>

  
         <div></div>
    <div></div>
   
   
    <script>
        /*
        
        */

        // 1、获取元素
        var oBtn = document.getElementsByTagName('button');
        var oDiv = document.querySelectorAll('div');

        // 假设 开关的状态 true  false
        // var tag = true;

        for(var i=0;i<oBtn.length;i++){//i =2

            // 给按钮添加自定义属性
            oBtn[i].tag =  true; 
            // 自定义索引 ---保留下标
            oBtn[i].index = i;
            oBtn[i].onclick = function(){
                // 3、做什么事
                if(this.tag == true){
                    oDiv[this.index].style.display = 'none';
                    this.tag = false;
                }else{
                    oDiv[this.index].style.display = 'block';
                    this.tag = true;
                }
            }
        }
    </script>
</body>

</html>